<template>
  <el-card>
    <div slot="header" class="clearfix">
      <span>城市列表</span>
    </div>

    <!-- 表格 -->
    <el-table :data="tableData" stripe style="width: 100%" v-loading="loading">
          <el-table-column header-align="center" prop="cityId" align="center" label="城市Id"></el-table-column>
      <el-table-column header-align="center" align="center" prop="name" label="城市名称" width="180"></el-table-column>
      <el-table-column header-align="center" align="center" prop="pinyin" label="城市拼音" width="180"></el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        :formatter="formatter"
        prop="isHot"
        label="热门城市">
      </el-table-column>

      <!-- 操作 -->
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <!-- scope.row是本行对应的数据 -->
          <router-link :to="`/city/edit/${scope.row.cityId}`">
            <el-button type="text" size="small">编辑</el-button>
          </router-link>
          <router-link :to="`/area/add/${scope.row.cityId}/${scope.row.name}`">
          <el-button type="text" size="small">添加区域</el-button>
          </router-link>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row v-if="value">
      <el-pagination
        @current-change="changepage"
        :page-size="pageSize"
        layout="prev, pager, next"
        hide-on-single-page="value"
        :total="total"
        class="pagination"
      ></el-pagination>
    </el-row>
  </el-card>
</template>

<script>
import { Loading } from "element-ui";
export default {
  data() {
    return {
      value:false,
      total:false,
      loading: false,
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total:0
    };
  },
  created() {
    this.getList(this.pageNum);
  },
    if () {
      
    },
  methods: {

    async getList(pageNum) {
      let url = "/city/getList";
      this.loading = true;
      try {
        let res = await this.$axios.post(url, {
          pageSize: this.pageSize,
          pageNum,
        });
        this.tableData = res.cities;
        this.total = res.total;
      } catch (error) {}
      this.loading = false;

      //分页少于2页时不显示页码
      if(this.total>this.pageSize){
        this.value=true
      }
    },
    
   async changepage(pageNum) {
    let dete=await this.getList(pageNum);
    },

    //格式化热门城市
    formatter(row, column, cellValue, index) {
      let text = cellValue? "是" : "否";
      return text;
    },

    //删除
    handleDelete(index, row) {
      let btn = confirm("是否要删除");
      if (btn == true) {
        let { cityId } = row;
        let url = "/city/delById";
        let data = { cityId };
        let res = this.$axios.post(url, data);
        this.tableData.splice(index, 1);
        this.$message({
          message: "删除成功"
        });
      } else {
      }
    }
  }
};
</script>

<style>
element.style {
  height: 85px;
}
.cell,
.el-table th div {
  padding-right: 0px !important;
  padding-left: 0px !important;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
